package com.chexiehui.jc.mvp.home;

import com.blankj.utilcode.util.StringUtils;
import com.chexiehui.jc.base.ApiCallback;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BasePresenter;

/**
 * @Description 贷款额度测算
 * @Auth SongJun
 * @Date 2019/11/20 10:05
 **/
public class LoanQuotaCalculatePresenter extends BasePresenter<LoanQuotaCalculateCovenant.View, LoanQuotaCalculateCovenant.Stores>
        implements LoanQuotaCalculateCovenant.Presenter {

    public LoanQuotaCalculatePresenter(LoanQuotaCalculateCovenant.View view) {
        attachView(view);
    }

    @Override
    public void sendCode() {
        if(StringUtils.isTrimEmpty(mvpView.getPersonMobile())){
            mvpView.showToast("请输入手机号");
            return;
        }
        addSubscription(appStores.sendCode(mvpView.getPersonMobile()),
                new ApiCallback<BaseModel<Object>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<Object> model) {
                        if (model.getResultCode() == 0) {
                            mvpView.onSendCodeSuccess(model);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onSendCodeFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void doApply() {
        if(StringUtils.isTrimEmpty(mvpView.getCarModelId())){
            mvpView.showToast("请选择车型");
            return;
        }
        if(StringUtils.isTrimEmpty(mvpView.getPersonName())){
            mvpView.showToast("请输入姓名");
            return;
        }
        if(StringUtils.isTrimEmpty(mvpView.getPersonMobile())){
            mvpView.showToast("请输入手机号");
            return;
        }
        if(StringUtils.isTrimEmpty(mvpView.getPhoneCode())){
            mvpView.showToast("请输入验证码");
            return;
        }
        if(StringUtils.isTrimEmpty(mvpView.getPersonCity())){
            mvpView.showToast("请选择购车城市");
            return;
        }
        mvpView.showLoading("正在提交");
        addSubscription(appStores.checkCode(mvpView.getPersonMobile(), mvpView.getPhoneCode()),
                new ApiCallback<BaseModel<Object>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<Object> model) {
                        if (model.getResultCode() == 0) {
                            doNormalApply();
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.hide();
                        mvpView.onDoApplyFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    private void doNormalApply() {
        addSubscription(appStores.doApply(mvpView.getCarModelId(), mvpView.getPersonIncome(),
                mvpView.getPersonSecurity(), mvpView.getPersonTime(),  mvpView.getPersonSeeCar(),
                mvpView.getPersonName(),mvpView.getPersonMobile(),mvpView.getPersonCity(),mvpView.getSeeCar()),
                new ApiCallback<BaseModel<Object>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<Object> model) {
                        if (model.getResultCode() == 0) {
                            mvpView.hide();
                            mvpView.onDoApplySuccess(model);
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.hide();
                        mvpView.onDoApplyFailure(new BaseModel<>(code, msg));
                    }
                });
    }

}